<template>
  <view :class="classNameList">
    <view class="head">
      <view class="title">
        {{header.title}}
      </view>
      <view v-if="sales" class="desc">
        {{sales}}
      </view>
    </view>

    <view class="content">
      <view class="scroll-box">
        <view class="li-all">
          <view class="avatar">···</view>
        </view>
        <view class="ul" :style="[ulStyle]">
          <view :class="['li', {'active': currentIndex === itemIndex}, {'fade-out': index === itemIndex || index === itemIndex+1 }]" v-for="(item,itemIndex) in renderList" :key="itemIndex">
            <view class="avatar">
              <x-image :src="item.avatar" />
            </view>
            <view class="name">{{item.name}}</view>
          </view>
        </view>
      </view>

      <view :class="['latest-user', {fadeIn: isFadeIn}]" @animationend="onAnimationend">
        <view class="avatar">
          <x-image :src="currentUser.avatar" />
        </view>
        <view class="name">{{currentUser.name}}正在下单</view>
      </view>

      <view class="button" @click="handledClick">
        {{ header.desc || '立即下单享优惠' }}
      </view>
    </view>
  </view>
</template>

<script>
import XImage from '@/components/x-image';

export default {
  name: 'XCarousel',
  components: {
    XImage
  },
  props: {
    // 头部
    header: {
      type: [Object, String],
      default: () => null
    },
    // 数据源
    list: {
      type: Array,
      default: () => []
    },
    // 自动播放的间隔（ms）
    interval: {
      type: Number,
      default: 3000
    },
    max: {
      type: Number,
      default: 20
    },
    sales: {
      type: String,
      default: ''
    },
    isNewTemplate: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isFadeIn: false,
      renderList: [],
      currentUserOpacity: 0,
      initialNum: 6,
      index: 0,
      timeout: null
    };
  },
  computed: {
    classNameList() {
      const arr = ['x-carousel'];
      if (this.isNewTemplate) {
        arr.push('is-new-template');
      }

      return arr.join(' ');
    },
    ulStyle() {
      return {
        transform: `translateX(${-this.index * 100 / 6}%)`,
        transition: this.transition
      };
    },
    currentUser() {
      return this.renderList[this.currentIndex];
    },

    currentIndex() {
      return this.index + this.initialNum - 1;
    }
  },
  created() {

  },
  mounted() {
    if (this.list.length < this.initialNum) {
      return;
    }
    this.renderList = [...this.list];
    if (this.list.length > this.max) {
      this.renderList = this.list.slice(0, this.max);
    }

    this.renderList.push(...this.list.slice(0, this.initialNum));
    this.start();
  },
  destroyed() {
    if (this.timeout) {
      clearTimeout(this.timeout);
    }
  },
  methods: {
    start() {
      this.timeout = setTimeout(() => {
        if (this.index === this.renderList.length - this.initialNum) {
          this.transition = 'none';
          this.index = 0;
        } else {
          this.transition = 'all 0.4s linear';
          this.index++;
        }
        this.isFadeIn = true;
        this.start();
      }, this.index === 0 ? 0 : this.interval);
    },
    handledClick() {
      this.$emit('buy');
    },
    onAnimationend() {
      this.isFadeIn = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.x-carousel{
  margin: 16px 0;
  background: #FFFFFF;

  &.is-new-template{
    margin: 16px var(--padding);
    border-radius: 20px;
    background: #F8F8F8;
  }
  .head{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 28px 24px;
    border-bottom: 1px solid #E6E6E6;
    .title{
      font-size: 32px;
      color: #323232;
    }
    .desc{
      font-size: 26px;
      color: #999999;
    }
  }

  .content{
    padding: 24px;

    .scroll-box{
      position: relative;
      overflow: hidden;
      .li-all{
        position: absolute;
        z-index: 1;
        top: 0;
        left: 0;
        width: calc(100% / 6);
        padding-top: 8px;
        .avatar{
          margin: auto;
          width: 70px;
          height: 70px;
          background: #F6EEEA;
          border-radius: 50%;
          color: #EC612C;
          line-height: 70px;
          text-align: center;
        }
      }
      .ul{
        position: relative;
        display: flex;
        align-items: center;
        .li{
          padding-top: 8px;
          width: calc(100% / 6);
          flex: none;
          .avatar{
            position: relative;
            margin: auto;
            width: 70px;
            height: 70px;
            background: #CC8E6F;
            border-radius: 50%;
          }
          .name{
            margin-top: 10px;
            text-align: center;
            font-size: 24px;
            color: #323232;
          }

          &.fade-out{
            animation: fadeOut 0.8s linear forwards;
          }

          &.active {
            .avatar{
              animation: zoomIn .6s linear;
              &:before{
                content: '';
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%) scale(1.1);
                width: 100%;
                height: 100%;
                border: 2px dashed #F0AD8A;
                border-radius: 50%;
                opacity: 0;
                animation: rotate 2s linear;
                animation-delay: 0.6s;
              }
            }
          }
        }
      }
    }

    .latest-user{
      padding: 40px 0;
      display: flex;
      align-items: center;
      justify-content: center;
      opacity: 0;
      .avatar{
        width: 28px; height: 28px; border-radius: 50%; overflow: hidden; background-color: #FFFFFF;
      }
      .name{
        color: #676767;
        font-size: 24px;
      }
    }

    .button{
      padding: 20px;
      font-size: 28px;
      color: #EE7D47;
      text-align: center;
      border: 1px solid #EE7D47;
      border-radius: 100px;
      background: #FFFFFF;
    }
  }
}

.fadeIn{
  animation: fadeIn 2.5s ease forwards;
}
@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  30%{
    opacity: 1;
  }
  80%{
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
@keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
@keyframes rotate {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.1) rotate(0);
  }
  50%{
    transform: translate(-50%, -50%) scale(1.1) rotate(0.5turn);
    opacity: 1;
  }
  70%{
    transform: translate(-50%, -50%) scale(1.1) rotate(0.6turn);
    opacity: 0;
  }
  100% {
    transform: translate(-50%, -50%) scale(1.1) rotate(0.5turn);
    opacity: 0;
  }
}

@keyframes zoomIn {
  0% {
    transform: scale(0.3);
  }
  100% {
    transform: scale(1);
  }
}
</style>
